(function(){
	'use strict';
	angular.module('controllers.workOrderData.useAndHandle',[])
	.controller('UseAndHandleController',UseAndHandleController);
	/**
	 * 统计维度：
	 * t1001：工单
	 * 5007：事件单
	 * 24807：变更单
	 * 15601：问题单
	 * t1006：工单解决率
	 * t1007：及时处理率
	 */
	function UseAndHandleController($scope,workOrderDataService,changeMgmtFactory,CoreLoading,PublicFn,$timeout,COREDATA,useAndHandleFactory,WorkOrderType){
		//初始化统计维度
		$scope.initQuery = function(){
			workOrderDataService.queryStatisticType({code:'1'},function(res){
				$scope.statisticTypes = res.data;
				$scope.search();
			});
		};
		//工单数据
		$scope.workOrderTypes = WorkOrderType;

		$scope.flag = {
			state: true,
			show: 'view',
			arrow: false,
		};
		//默认时间为天
		$scope.sendData = useAndHandleFactory.pageBindData();
		$scope.sendData.statisticsType = 't1001';
		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/useAndHandleCondition/export';

		var myEcharts = echarts.init(document.getElementById('doc'));
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var option = {};
			var timeTitle,code,statisticsTypeName,workOrderTypeName;
			if(!$scope.flag.state){
				$scope.send = useAndHandleFactory.fmtSendData($scope.sendData);
				timeTitle = PublicFn.fmtDateTitle($scope.send,'timeType');
				CoreLoading.setState(true);
				useAndHandleFactory.query($scope.send,function(res){
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						code = $scope.send.statisticsType;
						$scope.items = workOrderDataService.getArrayData(res.data);
						$scope.arrs = res.data;
						//统计维度名称
						statisticsTypeName = workOrderDataService.getName(code,$scope.statisticTypes,'statisticsTypeCode','statisticsTypeName');
						//工单类型名称
						workOrderTypeName = workOrderDataService.getName($scope.send.changeTypeCode,$scope.workOrderTypes,'workOrderTypeCode','workOrderTypeName');
						//标题
						$scope.send.title = useAndHandleFactory.getTitle(code,timeTitle,statisticsTypeName,workOrderTypeName);
						option = workOrderDataService.getEchartsLineData($scope.items,res.data,$scope.send.title,'percentage');
						if(code === 't1006' || code === 't1007'){
							option.tooltip.formatter = workOrderDataService.getFormatterFn('%');
							option.yAxis[0] = workOrderDataService.formatterYAxis('','{value}%');
						}else{
							option.tooltip.formatter = workOrderDataService.getFormatterFn('个');
							option.yAxis[0] = workOrderDataService.formatterYAxis('(单位:个)','');
						}
						myEcharts.setOption(option);
						$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						$scope.flag.arrow = true;
						$scope.flag.show = 'view';
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};
		$scope.changeMeasureIndex = function(code){
			code === 't1006' || code === 't1007' ? $scope.sendData.changeTypeCode = '15601' : $scope.sendData.changeTypeCode = '';
		};
		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
			newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});
	}
	UseAndHandleController.$inject = ['$scope','workOrderDataService','changeMgmtFactory','CoreLoading','PublicFn','$timeout','COREDATA','useAndHandleFactory','WorkOrderType'];
})();